package com.lms.busi.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lms.busi.common.mapper.TLmsOrganizationLeaderMapper;
import com.lms.busi.common.service.ITLmsOrganizationLeaderService;
import com.lms.common.entity.TLmsOrganizationLeader;
import com.lms.common.web.service.impl.WrapperServiceImpl;
import com.lms.common.web.utils.CheckEntityResult;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Map;

@Service
public class TLmsOrganizationLeaderServiceImpl extends WrapperServiceImpl<TLmsOrganizationLeaderMapper, TLmsOrganizationLeader> implements ITLmsOrganizationLeaderService {
    @Override
    public IPage<?> queryMapper(Page<TLmsOrganizationLeader> page, Map<String, Object> p) {
        return null;
    }
    @Override
    public CheckEntityResult beforeUpdate(TLmsOrganizationLeader entity, Serializable id) {
        removeOldLeaderData(entity.getOrgId());
        return CheckEntityResult.getInstance();
    }

    @Override
    public CheckEntityResult beforeInsert(TLmsOrganizationLeader entity) {
        removeOldLeaderData(entity.getOrgId());
        return CheckEntityResult.getInstance();
    }

    private void removeOldLeaderData(String orgId) {
        QueryWrapper<TLmsOrganizationLeader> params = new QueryWrapper<>();
        params.eq("org_id", orgId);
        this.remove(params);
    }
}
